package com.ywd.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywd.platform.entity.VehicleTrackRecordEntity;
import com.ywd.platform.mapper.VehicleTrackRecordMapper;
import com.ywd.platform.netty.jt080.util.CoordinateConverter;
import com.ywd.platform.vo.PointVo;
import com.ywd.platform.vo.VehicleTrackRecordOutVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * YWD
 * 2025/8/21
 */
@AllArgsConstructor
@Service
public class VehicleTrackRecordServiceImpl extends ServiceImpl<VehicleTrackRecordMapper, VehicleTrackRecordEntity> {

    private final DeviceServiceImpl deviceService;

    public PointVo getVehicleTrackRecord() {
        PointVo pointVo = this.baseMapper.getVehicleTrackRecord();
        if (ObjectUtil.isEmpty(pointVo) || ObjectUtil.isNull(pointVo.getLatitude()) || ObjectUtil.isNull(pointVo.getLongitude())) {
            return null;
        }
//        //转换地址坐标
//         pointVo = CoordinateConverter.wgs84ToGcj022(pointVo.getLongitude(),
//                 pointVo.getLatitude());
        return pointVo;
    }

    public List<VehicleTrackRecordOutVo> getVehicleTrackRecordList() {
        List<PointVo> list = this.baseMapper.getVehicleTrackRecordList();
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        //坐标转化
        //当前只有两台设备
        List<VehicleTrackRecordOutVo> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if(i==0){
                PointVo pointVo = list.getFirst();
                VehicleTrackRecordOutVo converted = CoordinateConverter.wgs84ToGcj022(pointVo.getLongitude(), pointVo.getLatitude());

//                VehicleTrackRecordOutVo converted =  new VehicleTrackRecordOutVo();
//                converted.setLongitude(new BigDecimal("103.14363956451416"));
//                converted.setLatitude(new BigDecimal("30.03569958552638"));

                converted.setType("ONE");
                list1.add(converted);
            }else {
                PointVo pointVo = list.get(1);
                VehicleTrackRecordOutVo converted = CoordinateConverter.wgs84ToGcj022(pointVo.getLongitude(), pointVo.getLatitude());
                converted.setType("TWO");
                list1.add(converted);
            }
        }
        return list1;
    }

    public List<VehicleTrackRecordEntity> getRecordTimeToDeviceScheduler(Long deviceId, LocalDateTime time) {
        LambdaQueryWrapper<VehicleTrackRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(VehicleTrackRecordEntity::getDeviceId, deviceId)
                .ge(VehicleTrackRecordEntity::getRecordTime, time);
        return list(queryWrapper);
    }


}
